package com.promfy.util;

import android.util.Log;

public class NumberConvertUtil {

	static final String TAG = "shareMeAirPlay";
	public static void log(String msg) {
		Log.d(TAG, ".... " + msg);
	}

		static public void clearBuffer(int[] data, int len) {
			for(int i = 0; i < len; i++) {
				data[i] = 0;
			}		
		}

		static public void clearBuffer(byte[] data, int len) {
			for(int i = 0; i < len; i++) {
				data[i] = 0;
			}		
		}

		static public void clearBuffer(short[] data, int len) {
			for(int i = 0; i < len; i++) {
				data[i] = 0;
			}		
		}

		public static String byteArrayToString(byte[] arr, int len) {
		       StringBuffer sb = new StringBuffer();
		        for (int i = 0; i < len; i++) {
		            String hex = Integer.toHexString(arr[i] & 0xFF);
		            if (hex.length() == 1) {
		                hex = '0' + hex;
		            }
		            sb.append(hex.toLowerCase());
		        }
		        return sb.toString();
			}

		// add a blank space for clearing display in log 
		public static String byteArray2String(byte[] arr, int len) {
	       StringBuffer sb = new StringBuffer();
	        for (int i = 0; i < len; i++) {
	            String hex = Integer.toHexString(arr[i] & 0xFF);
	            if (hex.length() == 1) {
	                hex = '0' + hex;
	            }
	            sb.append(hex.toLowerCase() + " ");
	        }
	        return sb.toString();
		}

		public static String byteArrayToString(byte[] input, int start, int len) {
			byte[] temp = new byte[1000];

			System.arraycopy(input, start, temp, 0, len);
			String s = NumberConvertUtil.byteArray2String(temp, len);
			return s;
		}
		
		static public void clearBuffer(char[] data, int len) {
			for(int i = 0; i < len; i++) {
				data[i] = 0;
			}		
		}

		public static String intArray2String(int[] arr) {
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < arr.length; i++) { 
				sb.append(Integer.toHexString(arr[i]));
				if (i < arr.length - 1)
					sb.append(":");
			}
			return sb.toString(); 
		}

		public static String charArray2String(char[] arr) {
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < arr.length; i++) { 
				sb.append(Integer.toHexString(arr[i]));
				if (i < arr.length - 1)
					sb.append(":");
			}
			return sb.toString(); 
		}
		
	public static byte[] hex2byte(String str) { // 字符串转二进制
		if (str == null)	return null;
		str = str.trim();
		int len = str.length();
		if (len == 0 || len % 2 == 1)	return null;
		byte[] b = new byte[len / 2];
		try {
			for (int i = 0; i < str.length(); i += 2) {
				b[i / 2] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();
			}
			return b;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	 public static String bytesToHexString(byte[] src){  
	        StringBuilder stringBuilder = new StringBuilder("");  
	        if (src == null || src.length <= 0) {  
	            return null;  
	        }  
	        for (int i = 0; i < src.length; i++) {  
	            int v = src[i] & 0xFF;  
	            String hv = Integer.toHexString(v);  
	            if (hv.length() < 2) {  
	                stringBuilder.append(0);  
	            }  
	            stringBuilder.append(hv);  
	        }  
	        return stringBuilder.toString();  
	    }  
	 public static String byte2Hex(byte[] src, int len){  
		 StringBuilder stringBuilder = new StringBuilder("");  
		 if (src == null || len <= 0) {  
			 return null;  
		 }  
		 for (int i = 0; i < len; i++) {  
			 int v = src[i] & 0xFF;  
			 String hv = Integer.toHexString(v);  
			 if (hv.length() < 2) {  
				 stringBuilder.append(0);  
			 }  
			 stringBuilder.append(hv);  
		 }  
		 return stringBuilder.toString();  
	 }  
}
